package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Prizeoperater extends AbstractActiviityOperater{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //判断当前奖品状态是否全部抽完
Long activityId= convertActivityStatusDTO.getActivityId();
Long prizeId= convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizStatus=convertActivityStatusDTO.getTargetPrizeStatus();
        if(null==prizeId||
                null==targetPrizStatus
        ||null==activityId){
            return false;
        }

        ActivityPrizeDO activityPrizeDO=activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(null==activityPrizeDO){
            return false;
        }
        //判断当前奖品状态和目标奖品状态是否一直
if(targetPrizStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())){
    return false;
}

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId()
                    ,convertActivityStatusDTO.getPrizeId(),convertActivityStatusDTO.getTargetPrizeStatus().name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
